package com.arch.dev.server;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;

public class EmailServiceImpl {

	public static EmailServiceImpl emailServiceImpl = new EmailServiceImpl();

	private Properties mailProps;

	public static String FROM_MAIL_ID = "noreply@costexpense.com";

	public static String[] TO_EMAIL_ID = { "little25goddess@gmail.com",
			"narain.ankur@gmail.com" };

	@Autowired
	private JavaMailSender mailSender;

	@Autowired
	VelocityEngine velocityEngine;

	private EmailServiceImpl() {

	}

	public static EmailServiceImpl getInstance() {
		return emailServiceImpl;
	}

	public Properties getMailProps() {
		return mailProps;
	}

	public void setMailProps(Properties mailProps) {
		this.mailProps = mailProps;
	}

	public void sendSimpleEmail(String subject, String emailContent) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(FROM_MAIL_ID);
		message.setTo(TO_EMAIL_ID);
		message.setSubject(subject);						
		message.setText(emailContent);
		mailSender.send(message);
	}

	public void sendEmailWithAttachment(String subject, String emailContent,
			Map<String, String> attachmentMap) {
		try {
			configureMailProperties();
			MimeMessage message = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(FROM_MAIL_ID);
			helper.setTo(TO_EMAIL_ID);
			helper.setSubject(subject);
			helper.setText(configureVelocityTemplate(), true);
			Set<String> keySet = attachmentMap.keySet();
			Iterator<String> itr = keySet.iterator();
			FileSystemResource fsr = null;
			while (itr.hasNext()) {
				String key = itr.next();
				String path = attachmentMap.get(key);
				fsr = new FileSystemResource(path);
				helper.addAttachment(key, fsr);
			}
			mailSender.send(message);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void configureMailProperties() {
		JavaMailSenderImpl jmi = ((JavaMailSenderImpl) mailSender);
		jmi.setJavaMailProperties(mailProps);
	}

	private String emailVelocityTemplate = "emailTemplate.vm";

	public void setVelocityTemplate(String path) {
		emailVelocityTemplate = "emailTemplate.vm";
	}

	private String configureVelocityTemplate() {
		Map<String, String> model = new HashMap<String, String>();
		model.put("recipientName", "");
		model.put("month", getMonth(Calendar.getInstance().get(Calendar.MONTH)));
		return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
				emailVelocityTemplate, model);
	}

	private String getMonth(int mnth) {
		String month = "";
		switch (mnth) {
		case 0:
			month = "January";
			break;
		case 1:
			month = "February";
			break;
		case 2:
			month = "March";
			break;
		case 3:
			month = "April";
			break;
		case 4:
			month = "May";
			break;
		case 5:
			month = "June";
			break;
		case 6:
			month = "July";
		case 7:
			month = "August";
			break;
		case 8:
			month = "September";
			break;
		case 9:
			month = "October";
			break;
		case 10:
			month = "November";
			break;
		case 11:
			month = "December";
			break;

		}
		return month;
	}

}
